package com.tedu.infrastructure.converter;

import com.tedu.domain.bo.ContentListBO;
import com.tedu.po.Content;
import com.tedu.protocol.param.ContentParam;
import com.tedu.protocol.param.ContentUpdateParam;
import org.springframework.beans.BeanUtils;
import com.tedu.domain.bo.ContentBO;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class ContentConverter {

    public  Content paramToPo(ContentParam contentParam) {
        Content content = new Content();
        BeanUtils.copyProperties(contentParam, content);
        Instant now = Instant.now();//利用Instant获取时间戳可以省略时区步骤，呦西
        long currentTimeMillis = now.toEpochMilli();
        content.setGmtCreate(currentTimeMillis);
        content.setGmtModified(currentTimeMillis);
        return content;
    }


    public  Content paramToPo(ContentUpdateParam contentUpdateParam) {
        Content content = new Content();
        BeanUtils.copyProperties(contentUpdateParam, content);
        Instant now = Instant.now();
        long currentTimeMillis = now.toEpochMilli();
        content.setGmtModified(currentTimeMillis);
        return content;
    }

    /**
     * 将每个 content 对象都映射为一个新的 ContentListBO 对象，
     * 在映射的过程中使用 BeanUtils
     * 将 content 的属性值复制到 ContentListBO 中，
     * 最终将新的对象收集到 contentBoList 列表中
     * @param list
     * @return
     */
    public  List<ContentListBO> poListToBoList(List<Content> list) {
        List<ContentListBO> contentBoList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            contentBoList = list.stream()
                    .map(content -> {
                        ContentListBO contentBo = new ContentListBO();
                        BeanUtils.copyProperties(content, contentBo);
                        return contentBo;
                    })
                    .collect(Collectors.toList());
        }
        return contentBoList;
    }


    public  ContentBO poToBo(Content content) {
        ContentBO contentBO = new ContentBO();
        BeanUtils.copyProperties(content, contentBO);
        return contentBO;
    }
}
